<?php
class Home extends PageBaseList{

    public function onLoad($param){
	   parent::onLoad($param);
        $this->ClassName = "Menu";
        //Las siguientes dos líneas son para que el formulario no haga submit
        $form = $this->getForm("frmMain");
        $form->Attributes->add("OnSubmit","return false;");

        if(!$this->IsPostBack){
        	$this->Refresh(0, $this->dgDatos->PageSize);
        }

	}

    public function dgDatos_OnPageIndexChanged($sender,$param){
		$this->dgDatos->CurrentPageIndex	= 	$param->NewPageIndex;
		$pageSize 							= 	$this->dgDatos->PageSize;
		$offset 							= 	$param->NewPageIndex*$pageSize;
		$this->dgDatos->DataSource 			= 	$this->Refresh($offset, $pageSize);
		$this->dgDatos->dataBind();
    }

    public function dgDatos_OnPagerCreated($sender,$param){
		$style			=	$param->Pager->DataGrid->getPagerStyle();
		$pageCount		=	$param->Pager->DataGrid->getPageCount();
		$pageIndex		=	$param->Pager->DataGrid->getCurrentPageIndex()+1;
		$maxButtonCount	=	$style->getPageButtonCount();
		$buttonCount	=	(($maxButtonCount > $pageCount) ? $pageCount : $maxButtonCount);
		$startPageIndex	=	1;
		$endPageIndex	=	$buttonCount;

		if($pageIndex > $endPageIndex){
			$startPageIndex	=	((int)(($pageIndex - 1) / $maxButtonCount)) * $maxButtonCount + 1;

			if(($endPageIndex = $startPageIndex + $maxButtonCount - 1) > $pageCount){
				$endPageIndex	=	$pageCount;
			}

			if($endPageIndex - $startPageIndex + 1 < $maxButtonCount){

                if(($startPageIndex = $endPageIndex - $maxButtonCount + 1) < 1){
					$startPageIndex	=	1;
				}

			}
		}

		$param->Pager->Controls->insertAt(0,"P&aacute;ginas " . $startPageIndex . " a " . $endPageIndex . " de " . $pageCount . ": ");
    }

	protected function Refresh($offset, $rows){
        $filtro = $this->txtBusqueda->Text;

        $propelDataSource = new PropelDataSource(
		"Menu",
		"",
		MethodType::Custom,
		"Menu2");

        $cant = $propelDataSource->CreateDataSource($filtro, "Count");
        $this->dgDatos->VirtualItemCount = $cant;
        $c = new Criteria();
		$c->setOffset($offset);
        $c->setLimit($rows);
		$this->dgDatos->DataSource = $propelDataSource->CreateDataSource($filtro, $c);
		$this->dgDatos->dataBind();
    }

    public function btnBuscar_OnClick($sender, $param){
        $this->Refresh(0, $this->dgDatos->PageSize);

        if($this->dgDatos->Items->Count>0){
            $this->lblBusqueda->Text = "";
        }
        else{
            $this->lblBusqueda->Text = "No se han encontrado datos para la consulta especificada.";
        }

    }

    public function btnVerTodos_OnClick($sender, $param){
        $this->txtBusqueda->Text = "";
        $this->lblBusqueda->Text = "";
        $this->Refresh(0, $this->dgDatos->PageSize);
    }

}
?>